package com.sayyi.validate.constraints.custom;


import com.sayyi.validate.valid.ValidateMapCache;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Collection;
import java.util.Map;

/**
 * @author xuchuang
 * date 2021/12/2 5:36 下午
 */
public class ExistCollectionValidator implements ConstraintValidator<Exist, Collection<?>> {

    private String cacheKey;

    @Override
    public void initialize(Exist constraintAnnotation) {
        cacheKey = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Collection value, ConstraintValidatorContext context) {
        Map<Object, Object> sets = ValidateMapCache.get(cacheKey);
        if (value == null || sets == null) {
            return true;
        }
        return sets.keySet().containsAll(value);
    }

}
